<?php
// +----------------------------------------------------------------------
// | OpenSociax [ open your team ! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.sociax.com.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: genixsoft.net <智士软件>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * Key-value存储引擎，用MySQL模拟memcache等key-value数据库写法
 * 以后可以切换到其他成熟数据库 或 amazon云计算平台
 +------------------------------------------------------------------------------
 * @category	core
 * @package		core
 * @author		liuxiaoqing <liuxiaoqing@thinksns.com>
 * @version		$0.1$ 完成基本数据结构
				$0.2$ 增加":查询语法"  list:key
				$0.3$ todo：增加多条查询 list1:key1,list2:key2,
				$0.4$ todo：增加模糊查询 list3:*,*:key,list1:ke3*;
 +------------------------------------------------------------------------------
 */
class XlogModel extends Model {

	protected	$tableName	=	'x_log';	// 数据库表名
	protected	$list_name	=	'global';	// 默认列表名
	protected	$fields		=	array (0 => 'id',1 => 'uid',2 => 'list',3 => 'key',4 => 'value',5 => 'mtime','_autoinc' => true,'_pk' => 'id');
   	
	// 写入列表
	public function lput($listName='',$listData=array()) {

		//初始化list_name
		$listName	=	$this->_strip_key($listName);

		//格式化数据
		if(is_array($listData)){

			$insert_sql	.=	"REPLACE INTO __TABLE__ (`uid`,`list`,`key`,`value`) VALUES ";

			foreach($listData as $key=>$data){
				$insert_sql	.=	" (".$_SESSION['mid'].",'$listName','$key','".serialize($data)."') ,";
			}

			$insert_sql	=	rtrim($insert_sql,',');

			//插入数据列表
			$result	=	$this->execute($insert_sql);

			return $result;
		}else{
			return false;
		}
	}
	
	// 读取列表
	public function lget($listName='') {

		//初始化list_name
		$listName	=	$this->_strip_key($listName);
		
		$map['`list`']		=	$listName;

		$result	=	$this->order('id ASC')->where($map)->findAll();
		
		if(!$result){

			return false;
		}else{

			foreach($result as $v){
				$datas[$v['key']]	=	unserialize($v['value']);
			}
		}
		
		return $datas;
	}
	
	// 写入数据
	public function put($key,$value='',$replace=false) {

		$key	=	$this->_strip_key($key);
		$keys	=	explode(':',$key);
		$data	=	serialize($value);
		if($replace){
			$insert_sql	=	"REPLACE INTO __TABLE__ ";
		}else{
			$insert_sql	=	"INSERT INTO __TABLE__ ";
		}

		$insert_sql	.=	"(`uid`,`list`,`key`,`value`) VALUES (".$_SESSION['mid'].",'$keys[0]','$keys[1]','$data') ";
		$result		=	$this->execute($insert_sql);
		//dump($this->getLastSql());
		return $result;
	}
	
	// 读取一条数据，todo：读取多条 list1:key1,list2:key2
	public function get($key) {
		$key	=	$this->_strip_key($key);
		$keys	=	explode(':',$key);
		
		$map['`list`']		=	$keys[0];
		$map['`key`']		=	$keys[1];

		$result	=	$this->where($map)->find();
		if(!$result){
			return false;
		}else{
			return unserialize($result['value']);
		}
	}
	
	// 修改数据数据
	public function save($key,$value='') {
		$result		=	$this->put($key,$value,true);
		return $result;
	}

	// 批量读取数据 非必要
	public function getAll($listName,$keys) {
		if($key){  //用于获取list下所有数据 Nonant
			$keysArray	=	$this->_parse_keys($keys);
			$map['`key`']		=	array('in',$keysArray);
		}
		
		$map['`list`']		=	$listName;
		

		$result	=	$this->where($map)->findAll();

		if(!$result){
			return false;
		}else{
			foreach($result as $v){
				$datas[$v['list']][$v['key']]	=	unserialize($v['value']);
			}
		}
		return $datas;
	}

	//解析过滤输入
	protected function _parse_keys($input=''){

		$output	=	'';

		if(is_array($input) || is_object($input)){

			foreach($input as $v){
				$output[]	=	$this->_strip_key($v);
			}
		}elseif(is_string($input)){

			$output[]	=	$this->_strip_key($input);
		}else{
			//异常处理
		}
		
		return $output;
	}
	
	//过滤key,允许格式 数字字母下划线，list:key 不允许出现html代码 和这些符号 ' " & * % ^ $ ? ->
	protected function _strip_key($key=''){
		if($key==''){

			return $this->list_name;
		}else{

			$key	=	strip_tags($key);
			$key	=	str_replace(array('\'','"','&','*','%','^','$','?','->'),'',$key);

			return $key;
		}
	}		
}
?>